package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.Feedback;
import com.dropdb.demo.bean.FeedbackExample;
import com.dropdb.demo.bean.bo.BaseData;
import com.dropdb.demo.mapper.FeedbackMapper;
import com.dropdb.demo.service.FeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author:tanxin Desc:
 * @date:2020/6/27 9:07
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Resource
    FeedbackMapper feedbackMapper;
    @Override
    public BaseData queryFeedback(Integer page, Integer limit, String sort, String order, Integer id,  String username) {

        FeedbackExample feedbackExample = new FeedbackExample();

        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();

        //条件查询
        if (id != null & username != null){
            criteria.andIdEqualTo(id).andUsernameEqualTo(username);
        }else if (id != null){
            criteria.andIdEqualTo(id);
        }else if (username != null){
            criteria.andUsernameEqualTo(username);
        }
        //设置排序方式
        feedbackExample.setOrderByClause(sort + " " + order);
        //分页
        PageHelper.startPage(page,limit);
        //查询list
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        //得到记录总数
        PageInfo<Feedback> pageInfo = new PageInfo<>(feedbacks);
        long total = pageInfo.getTotal();
        //返回总记录和list

        return new BaseData(total,feedbacks);
    }


    /**
     * 插入反馈信息
     * @param feedback 反馈
     * @param userId 用户id
     * @param username 用户名
     * @return
     */
    @Override
    public Integer submitFeedback(Feedback feedback, Integer userId, String username) {
        feedback.setUserId(userId);
        feedback.setUsername(username);
        Date date = new Date();
        feedback.setAddTime(date);
        feedback.setUpdateTime(date);
        feedback.setDeleted(false);
        Integer result = feedbackMapper.insertSelective(feedback);
        return result;
    }

}
